/****************************************************
	文件：StockSpider.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/7/23 10:9:21
	功能：股票数据爬虫
*****************************************************/

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace HTSpider
{
    //东方财富官网："http://www.eastmoney.com"
    //东方财富行情中心：http://quote.eastmoney.com/center/gridlist.html#hs_a_board

    public class StockSpider : SpiderBase
    {
        /// <summary>
        /// 准备 请求
        /// </summary>
        protected override List<Req> PrepareReq()
        {
            List<Req> reqLst = new List<Req>();
            int count = 80;//80
            for (int i = 0; i < count; i++)
            {
                Req req = new Req
                {
                    //Url = "https://www.lixinger.com/analytics/index/dashboard/value/list",
                    Url = string.Concat("http://59.push2.eastmoney.com/api/qt/clist/get?cb=jQuery1124011937096689881144_1594558124485&pn=", i.ToString(), "&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1594558124521"),
                    Encoding = Consts.EncodingType.UTF8,
                    Priority = Enums.ReqPriority.High,
                };
                reqLst.Add(req);
            }
            return reqLst;
        }
        /// <summary>
        /// 解析 响应
        /// </summary>
        public override void ParseRsp()
        {
            List<Rsp> rspLst = SpiderMgr.Instance.GetAllRsp();
            string content = rspLst[0].Content;
            //string pattern = "\"f14\":\"([\u4e00-\u9fa5]*)\",";
            string pattern1 = "({\"f1.*?[\u4e00-\u9fa5]*.*?\"f152\":\\d*})"; //解析每条股票数据
            List<string> stockInfoLst = HtmlParse.Matches(content, pattern1);
            Dictionary<string, List<string>> stockInfoDict = new Dictionary<string, List<string>>();
            List<string> stockPropertyLst = new List<string>();
            List<string> stockPropertyKeyValueLst = new List<string>();
            for (int i = 0; i < stockInfoLst.Count; i++)
            {
                stockPropertyLst.Clear();
                stockPropertyKeyValueLst.Clear();
                string stockInfo = HtmlParse.Replace(stockInfoLst[i], new string[2] { "{", "}" }, string.Empty);
                string stockName = HtmlParse.Match(stockInfoLst[i], "\"f14\":\"(.*?)\"");
                stockPropertyKeyValueLst = HtmlParse.Split(stockInfo, ',');
                for (int j = 0; j < stockPropertyKeyValueLst.Count; j++)
                {
                    string value = HtmlParse.Match(stockPropertyKeyValueLst[j], "\"f\\d{1,3}\":(.*),*?");
                    string stockProperty = HtmlParse.Replace(value, "\"", string.Empty);
                    if (string.IsNullOrEmpty(stockProperty) == false)
                    {
                        if (stockProperty == stockName) continue;
                        stockPropertyLst.Add(stockProperty);
                    }
                }
                if (stockInfoDict.ContainsKey(stockName) == false)
                {
                    stockInfoDict.Add(stockName, stockPropertyLst);
                }
            }
            foreach (var item in stockInfoDict)
            {
                Logger.Warning("股票名称：" + item.Key);
                for (int i = 0; i < item.Value.Count; i++)
                {
                    Logger.Log("股票属性：" + item.Value[i]);
                }
            }
        }

    }
}
